/**
 *  File: SendReChargeService.java
 *  Date         Author      Changes
 *  2016-10-11    wangtq       Create
 *
 */
package com.kunpu.mami.channel.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.kunpu.mami.channel.common.ResConstants;
import com.kunpu.mami.channel.exception.MamiiException;

/**
 * 宝付充值接口服务
 * @author wangtq
 * @date 2016-10-11
 * @version 0.1
 *
 */
@Service
public class SendReChargeService extends BaseSendService{
	
	private final Logger       logger = LoggerFactory.getLogger(this.getClass());
	
		/**
	    * 
	    * @param trans_id   交易流水号
	    * @param bind_id    银行返回bindId
	    * @param txn_amt    交易金额
	    * @param sms_code   短信码
	    * @param additional_info 
	    * @param req_reserved
	    * @return
	    */
	public void sendReCharge(String trans_id, String bind_id, String txn_amt, String sms_code, String additional_info, String req_reserved){
		logger.info("===================================== 渠道service sendReCharge 充值 start ,trans_id{},bind_id{},txn_amt{},sms_code{}:",trans_id,bind_id,txn_amt,sms_code);
		try{
			String dataContent = buildSendReChargeParam(trans_id, bind_id, txn_amt, sms_code, additional_info, req_reserved);
			String txn_sub_type = "04";// 交易子类
			send(dataContent, "sendReCharge",txn_sub_type);
		}catch(MamiiException me){
			logger.error("==================== sendReCharge 充值异常，异常信息为：",me.getMessage(),me);
			throw new MamiiException(me.getErrorCode() , me.getMessage());
		}catch(Exception e){
			logger.error("==================== sendReCharge 充值时异常，异常信息为：",e.getMessage(),e);
			throw new MamiiException(ResConstants.PAY_ANALYSIS_EXISTENT.getCode() , "充值时异常");
		}
		logger.info("====================================== 渠道service sendReCharge 充值 end ===============================");
	}
	
	
	/**
	 * 代收类交易
	 * @param trans_id 商户订单号
	 * @param bind_id 绑定标识号
	 * @param txn_amt 交易金额
	 * @param sms_code 短信验证码
	 * @param additional_info
	 * @param req_reserved
	 * @return
	 */
	private  String buildSendReChargeParam(String trans_id, String bind_id, String txn_amt, String sms_code, String additional_info, String req_reserved) {
		String trade_date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String txn_sub_type = "04";// 交易子类
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"txn_sub_type\":\"" + txn_sub_type + "\"");
		sb.append(",\"biz_type\":\"0000\"");// 接入类型默认0000
		sb.append(",\"terminal_id\":\"" + terminal_id + "\"");
		sb.append(",\"member_id\":\"" + member_id + "\"");
		sb.append(",\"trans_serial_no\":\"" + member_id + "" + trade_date + "\"");
		sb.append(",\"trans_id\":\"" + trans_id + "\"");
		sb.append(",\"bind_id\":\"" + bind_id + "\"");
		sb.append(",\"txn_amt\":\"" + txn_amt + "\"");
		sb.append(",\"sms_code\":\"" + sms_code + "\"");
		sb.append(",\"trade_date\":\"" + trade_date + "\"");
		sb.append(",\"additional_info\":\"" + additional_info + "\"");
		sb.append(",\"req_reserved\":\"" + req_reserved + "\"");
		sb.append("}");
		return sb.toString();
	}
		

	

	
}
